<%_
var i18nToLoad = [entityInstance];
for (var idx in fields) {
    if (fields[idx].fieldIsEnum == true) {
        i18nToLoad.push(fields[idx].enumInstance);
    }
}
_%>

currentAccount: any;
    <%= entityInstancePlural %>: <%= entityClass %>[];
    error: any;
    success: any;
    eventSubscriber: Subscription;
    <%_ if (searchEngine === 'elasticsearch') { _%>
    currentSearch: string;
    <%_ } _%>
    <%_ if (databaseType !== 'cassandra') { _%>
    routeData: any;
    links: any;
    totalItems: any;
    queryCount: any;
    itemsPerPage: any;
    page: any;
    predicate: any;
    previousPage: any;
    reverse: any;
    <%_ } _%>

    constructor(
        <%_ if (enableTranslation) { _%>
        private jhiLanguageService: JhiLanguageService,
        <%_ } _%>
        private <%= entityInstance %>Service: <%= entityClass %>Service,
        private parseLinks: ParseLinks,
        private alertService: AlertService,
        private principal: Principal,
        private activatedRoute: ActivatedRoute,
        <%_ if (fieldsContainBlob) { _%>
        private dataUtils: DataUtils,
        <%_ } _%>
        private router: Router,
        private eventManager: EventManager<%_ if (databaseType !== 'cassandra') { _%>,
        private paginationUtil: PaginationUtil,
        private paginationConfig: PaginationConfig
        <%_ } _%>
    ) {
        <%_ if (databaseType !== 'cassandra') { _%>
        this.itemsPerPage = ITEMS_PER_PAGE;
        this.routeData = this.activatedRoute.data.subscribe(data => {
            this.page = data['pagingParams'].page;
            this.previousPage = data['pagingParams'].page;
            this.reverse = data['pagingParams'].ascending;
            this.predicate = data['pagingParams'].predicate;
        });
        <%_ if (searchEngine === 'elasticsearch') { _%>
        this.currentSearch = activatedRoute.snapshot.params['search'] ? activatedRoute.snapshot.params['search'] : '';
        <%_ } _%>
        <%_ } _%>
        <%_ if (enableTranslation) { _%>
        this.jhiLanguageService.setLocations(<%- toArrayString(i18nToLoad) %>);
        <%_ } _%>
    }

    loadAll() {
        <%_ if (searchEngine === 'elasticsearch') { _%>
        if (this.currentSearch) {
            this.<%= entityInstance %>Service.search({
                query: this.currentSearch,
                size: this.itemsPerPage,
                sort: this.sort()}).subscribe(
                    (res: Response) => this.onSuccess(res.json(), res.headers),
                    (res: Response) => this.onError(res.json())
                );
            return;
        }
        <%_ } _%>
        this.<%= entityInstance %>Service.query(<%_ if (databaseType !== 'cassandra') { _%>{
            page: this.page - 1,
            size: this.itemsPerPage,
            sort: this.sort()}<%_ } _%>).subscribe(
            (res: Response) => this.onSuccess(res.json(), res.headers),
            (res: Response) => this.onError(res.json())
        );
    }
    <%_ if (databaseType !== 'cassandra') { _%>
    loadPage (page: number) {
        if (page !== this.previousPage) {
            this.previousPage = page;
            this.transition();
        }
    }
    transition() {
        this.router.navigate(['/<%= entityUrl %>'], {queryParams:
            {
                page: this.page,
                size: this.itemsPerPage,
                <%_ if (searchEngine == 'elasticsearch') { _%>
                search: this.currentSearch,
                <%_ } _%>
                sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
            }
        });
        this.loadAll();
    }

    clear() {
        this.page = 0;
        <%_ if(searchEngine === 'elasticsearch') { _%>
        this.currentSearch = '';
        <%_ } _%>
        this.router.navigate(['/<%= entityUrl %>', {
            page: this.page,
            sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
        }]);
        this.loadAll();
    }
    <%_ } _%>
    <%_ if(searchEngine === 'elasticsearch') { _%>
    search (query) {
        if (!query) {
            return this.clear();
        }
        this.page = 0;
        this.currentSearch = query;
        this.router.navigate(['/<%= entityUrl %>', {
            search: this.currentSearch,
            page: this.page,
            sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
        }]);
        this.loadAll();
    }
    <%_ } _%>
